﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace IKE_painter
{
    public class GraphComponentsSearch
    {
        public int Compute(Floyd_Warshall fw) //vraci pocet komponent grafu
        {
            fw.calcShortestPaths();
            int componentNumber = 0;
            int componentNodesCount=0;

            foreach (Node node in fw.nodes)
            {
                node.Component = int.MaxValue;
            }


            while (componentNodesCount < fw.nodes.Length) //dokud je pocet zakomponovanych nodu mensi nez vsech nodu (projdeme vsechny nody)
            {
                int i = 0;
                while (fw.nodes[i].Component != int.MaxValue) //najdeme prvni nezakomponovany node
                {
                    i++;
                }
                componentNumber++;
                fw.nodes[i].Component = componentNumber; //priradime vrcholu cislo komponenty
                componentNodesCount++; //zakomponovanych vrcholu mame o jedna vic

                for (int j = i+1; j < fw.nodes.Length; j++)
                {
                    if (i == j) continue;
                    if (fw.nodes[j].Component == int.MaxValue)
                    if (((fw.D[i,j]!=int.MaxValue))||((fw.D[j,i]!=int.MaxValue)))//pokud z nodu i do nodu j vede cesta pridame vrchol j do komponenty ve ktere je vrchol i
                    {
                        fw.nodes[j].Component = componentNumber;
                        componentNodesCount++;
                    }
                }
            }

            /*
            foreach (Edge edge in fw.edges)
            {
                edge.Component = edge.From.Component;
            }*/

            return componentNumber;
        }


    }
}
